package lumis.portal.audit;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import lumis.portal.PortalContext;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

/**
 * Represents an operation done (by an user or batch operation) in the Audit
 * aspect. 
 *
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@Entity
@GenericGenerator(name="portal-guid", strategy="lumis.portal.dao.hibernate.GuidGenerator")
@Table(name="lum_AuditEntry")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class AuditEntry
{
	@Id	@GeneratedValue(generator="portal-guid")
	@Column(name="auditEntryId", nullable=false)
	private String id;
	
	@Column(name="serverId")
	private String serverId;

	@Column(name="entryUserId")
	private String userId;
	
	@Column(name="entryUserLogin")
	private String userLogin;
	
	@Column(name="title")
	private String title;
	
	@Column(name="description")
	private String description;
	
	@Column(name="categoryId")
	private String categoryId;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="auditEntryTypeId")
	private AuditEntryType type;
	
	@Column(name="parentEntryId")
	private String parentId;
	
	/**
	 * The state of this entry.
	 * @since 6.0.0
	 */
	@Column(name= "entryState", columnDefinition="integer", nullable = false)
    @Type(type="lumis.portal.dao.hibernate.GenericEnumUserType", 
			parameters=
			{ 
				@Parameter(name="enumClass", value="lumis.portal.audit.AuditEntryState"),
				@Parameter(name="identifierMethod", value="ordinal")
			})
	private AuditEntryState entryState;
	
	/**
	 * The entry date. 
	 * @since 6.0.0
	 */
	@Column(name="entryDate")
	private Date creationDate;

	/**
	 * True if this entry must be saved, false otherwise.
	 * @since 6.0.0
	 */
	@Transient
	private boolean register;
	
	protected AuditEntry()
	{
		
	}
	
	protected AuditEntry(String userId,
						String userLogin,
						String title, 
						String description,						
						AuditEntryType type,
						boolean register)	
	{
		this.userId = userId;
		this.userLogin = userLogin;
		this.title = title;
		this.description = description;	
		this.categoryId = type.getCategory().getId();
		this.type = type;
		this.register = register;
		this.creationDate = new Date();
		this.serverId = PortalContext.getServerId();
	}
	
	/**
	 * Returns the entry id.
	 * @return	the entry id.
	 * @since 6.0.0
	 */
	protected String getId()
	{
		return id;
	}
	
	/**
	 * Sets the entry id.
	 * @param id	the entry id.
	 * @since 6.0.0
	 */
	void setId(String id)
	{
		this.id = id;
	}

	/**
	 * Returns the server id.
	 * @return	the server id.
	 * @since 6.0.0
	 */
	public String getServerId()
	{
		return serverId;
	}

	/**
	 * Sets the server id.
	 * @param serverId	the server id.
	 * @since 6.0.0
	 */
	public void setServerId(String serverId)
	{
		this.serverId = serverId;
	}
	
	/**
	 * Returns the user id.
	 * @return	the user id.
	 * @since 6.0.0
	 */
	protected String getUserId()
	{
		return userId;
	}

	/**
	 * Sets the user id.
	 * @param userId	the user id.
	 * @since 6.0.0
	 */
	protected void setUserId(String userId)
	{
		this.userId = userId;
	}

	/**
	 * Returns the user login.
	 * @return	the user login.
	 * @since 6.0.0
	 */
	protected String getUserLogin()
	{
		return userLogin;
	}

	/**
	 * Sets the user login.
	 * @param userLogin	user login.
	 * @since 6.0.0
	 */
	protected void setUserLogin(String userLogin)
	{
		this.userLogin = userLogin;
	}

	/**
	 * Returns the entry title.
	 * @return	the entry title.
	 * @since 6.0.0
	 */
	protected String getTitle()
	{
		return title;
	}

	/**
	 * Sets the entry title.
	 * @param title	the entry title.
	 * @since 6.0.0
	 */
	protected void setTitle(String title)
	{
		this.title = title;
	}
	
	/**
	 * Returns the entry description.
	 * @return	the entry description.
	 * @since 6.0.0
	 */
	protected String getDescription()
	{
		return description;
	}

	/**
	 * Sets the entry description.
	 * @param description	the entry description.
	 * @since 6.0.0
	 */
	protected void setDescription(String description)
	{
		this.description = description;
	}

	/**
	 * Returns the category id.
	 * @return	the category id.
	 * @since 6.0.0
	 */
	protected String getCategoryId()
	{
		return categoryId;
	}

	/**
	 * Sets the category id.
	 * @param categoryId	the category id.
	 * @since 6.0.0
	 */
	protected void setCategoryId(String categoryId)
	{
		this.categoryId = categoryId;
	}

	/**
	 * Returns the audit entry type.
	 * @return	the audit entry type.
	 * @since 6.0.0
	 */
	protected AuditEntryType getType()
	{
		return this.type;
	}

	/**
	 * Sets the audit entry type.
	 * @param type	the audit entry type.
	 * @since 6.0.0
	 */
	protected void setType(AuditEntryType type)
	{
		this.type = type;
	}

	/**
	 * Returns the id of the parent entry.
	 * @return	the id of the parent entry.
	 * @since 6.0.0
	 */
	protected String getParentId()
	{
		return parentId;
	}

	/**
	 * Sets the parent id.
	 * @param parentId	the parent id.
	 * @since 6.0.0
	 */
	protected void setParentId(String parentId)
	{
		this.parentId = parentId;
	}

	/**
	 * Returns the entry state.
	 * @return	the entry state.
	 * @since 6.0.0
	 */
	protected AuditEntryState getState()
	{
		return this.entryState;
	}

	/**
	 * Sets the entry state.
	 * @param state	the entry state.
	 * @since 6.0.0
	 */
	protected void setState(AuditEntryState state)
	{
		this.entryState = state;
	}

	/**
	 * Returns the entry creation date.
	 * @return	the entry creation date.
	 * @since 6.0.0
	 */
	protected Date getCreationDate()
	{
		return creationDate;
	}

	/**
	 * Sets the entry creation date.
	 * @param creationDate	the entry creation date.
	 * @since 6.0.0
	 */
	protected void setCreationDate(Date creationDate)
	{
		this.creationDate = creationDate;
	}
	
	/**
	 * Returns true if the entry must be saved, false otherwise.
	 * @return	true if the entry must be saved, false otherwise.
	 * @since 6.0.0
	 */
	protected boolean isRegister()
	{
		return register;
	}

	/**
	 * Change the flag to decide if the entry must be saved or not.
	 * @param register true if the entry must be saved, false otherwise.
	 * @since 6.0.0
	 */
	protected void setRegister(boolean register)
	{
		this.register = register;
	}
}